#include "magic_tower.h"
#include "ui_magic_tower.h"
#include <QRect>
#include <QPixmap>
#include <QPainter>


magic_tower::magic_tower(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::magic_tower)
{
    ui->setupUi(this);
    this->setGeometry(QRect(50,50,1200,810));

}

magic_tower::~magic_tower()
{
    delete ui;
}
void magic_tower::paintEvent(QPaintEvent *event)
{
    if(!IsRun)
    {InitBraver();}
    QPainter painter(this);
    //背景
    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(350,120,550,550);
    //格子
    painter.setPen(Qt::black);
    for(int i=1;i<=10;i++)
    {
        painter.drawLine(350,120+i*50,900,120+i*50);
    }
    for(int i=1;i<=10;i++)
    {
        painter.drawLine(350+i*50,120,350+i*50,670);
    }
    //画勇者
    painter.setPen(Qt::black);
    painter.setBrush(Qt::red);
    painter.drawRect(pos);
}
void magic_tower::InitBraver()
{
    level=1;
    hp=1000;
    atk=10;
    pdef=10;
    exp=gold=key1=key2=key3=0;
    QRect rect(600,620,50,50);
    pos=rect;
}
void magic_tower::keyPressEvent(QKeyEvent *event)
{
    IsRun=1;
    QKeyEvent *key=(QKeyEvent*)event;
    switch (key->key())
    {
    case Qt::Key_Up:
        pos.setTop(pos.top()-50);
        pos.setBottom(pos.bottom()-50);
        break;
    case Qt::Key_Down:
        pos.setTop(pos.top()+50);
        pos.setBottom(pos.bottom()+50);
        break;
    case Qt::Key_Left:
        pos.setLeft(pos.left()-50);
        pos.setRight(pos.right()-50);
        break;
    case Qt::Key_Right:
        pos.setLeft(pos.left()+50);
        pos.setRight(pos.right()+50);
        break;
    default:
        break;
    }
    repaint();
}
/*void magic_tower::update()
{
    switch (direction) {
    case 1:
        pos.setTop(pos.top()+50);
        pos.setBottom(pos.bottom()+50);
        break;
    case 2:
        pos.setTop(pos.top()-50);
        pos.setBottom(pos.bottom()-50);
        break;
    case 3:
        pos.setLeft(pos.top()-50);
        pos.setRight(pos.bottom()-50);
        break;
    case 4:
        pos.setLeft(pos.top()+50);
        pos.setRight(pos.bottom()+50);
        break;
    default:
        break;
    }
    update();
}*/
